package com.gy.edu.teach;

import android.content.Intent;
import android.graphics.Color;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.gy.edu.R;
import com.gy.edu.base.BaseActivity;
import com.gy.edu.home.FileDetailActivity;
import com.gy.edu.teach.adapter.TeachDetailAdapter;
import com.gy.edu.util.AnimUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 作者：gy on 16/12/24
 * 说明：微课程详情
 */
public class TeachDetailActivity extends BaseActivity {

    private ListView listView;
    private RelativeLayout rlNoamel;
    private LinearLayout llSelect;
    private TextView tvSelect, tvDelete;
    private TeachDetailAdapter adapter;
    private List<Integer> selects;//选中的条目
    private int isEdit;//是否编辑状态 1是
    private int isSelectAll;//是否为全选状态 1是

    @Override
    protected void initView() {
        setContentViewRes(R.layout.fragment_teach_home);
        setTitleMsg("汽车电器构造与维修");
        _baseRight_text.setText("取消");
        _baseRight_text.setVisibility(View.GONE);
        _imgRight.setImageResource(R.mipmap.download_del_icon);

        listView = (ListView) findViewById(R.id.teach_home_list);
        rlNoamel = (RelativeLayout) findViewById(R.id.teach_home_ll_normal);
        llSelect = (LinearLayout) findViewById(R.id.teach_home_ll_select);
        tvSelect = (TextView) findViewById(R.id.teach_home_tv_select);
        tvDelete = (TextView) findViewById(R.id.teach_home_tv_delete);
    }

    @Override
    protected void initData() {
        selects = new ArrayList<>();
        adapter = new TeachDetailAdapter(mContext);
    }


    @Override
    protected void setListener() {
        super.setListener();
        tvSelect.setOnClickListener(this);
        tvDelete.setOnClickListener(this);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                if (isEdit == 1) {
                    if (selects.contains(i)) {
                        selects.remove(Integer.valueOf(i));
                    } else {
                        selects.add(Integer.valueOf(i));
                    }
                    adapter.setSelects(selects);
                    if(selects.size() > 0){
                        tvDelete.setText("删除("+selects.size()+")");
                        tvDelete.setTextColor(Color.parseColor("#ff0000"));
                    } else {
                        tvDelete.setText("删除");
                        tvDelete.setTextColor(Color.parseColor("#999999"));
                    }
                } else {
                    Intent intent = new Intent(TeachDetailActivity.this, FileDetailActivity.class);
                    startActivity(intent);
                    AnimUtil.pushLeftInAndOut(TeachDetailActivity.this);
                }
            }
        });
    }

    @Override
    protected void fillData() {
        super.fillData();
        listView.setAdapter(adapter);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id._right:
                if (isEdit == 0) {
                    isEdit = 1;
                    _baseRight_text.setVisibility(View.VISIBLE);
                    _imgRight.setVisibility(View.GONE);
                    llSelect.setVisibility(View.VISIBLE);
                    rlNoamel.setVisibility(View.GONE);
                    tvDelete.setText("删除");
                    tvDelete.setTextColor(Color.parseColor("#999999"));
                } else {
                    isEdit = 0;
                    _baseRight_text.setVisibility(View.GONE);
                    _imgRight.setVisibility(View.VISIBLE);
                    llSelect.setVisibility(View.GONE);
                    rlNoamel.setVisibility(View.VISIBLE);
                    tvDelete.setText("删除");
                    tvDelete.setTextColor(Color.parseColor("#999999"));
                }
                adapter.setIsEdit(isEdit);
                break;
            case R.id.teach_home_tv_select:
                if (isSelectAll == 0) {
                    //全选
                    isSelectAll = 1;
                    for (int i = 0; i < 3; i++) {
                        selects.add(i);
                    }
                    adapter.setSelects(selects);
                    tvSelect.setText("取消全选");
                    tvDelete.setText("删除(3)");
                    tvDelete.setTextColor(Color.parseColor("#ff0000"));
                } else if (isSelectAll == 1) {
                    //取消全选
                    isSelectAll = 0;
                    selects.clear();
                    adapter.notifyDataSetChanged();
                    tvSelect.setText("全选");
                    tvDelete.setText("删除");
                    tvDelete.setTextColor(Color.parseColor("#999999"));
                }
                break;
            case R.id.teach_home_tv_delete:

                break;
        }
    }
}
